﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace QLFUI
{
    [ToolboxItem(true)]
    public partial class TextBoxEx : QLFControlBase
    {
        #region - 变量 -

        private Color _borderColor = Color.FromArgb(78, 160, 209);
        private Color _shadowColor = Color.FromArgb(133, 228, 255);
        private Image _foreImage = null;
        private bool _isFouse = false;
        private Color _backColor = Color.Transparent;
        private string _waterMark = null;
        private Color _waterMarkColor = Color.Silver;
        private Color _foreColor = Color.Black;
        private int _radius = 0;

        #endregion

        #region - 属性 -

        [Category("QLFUI"), Description("边框颜色,BorderStyle为FixedSingle有效")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set
            {
                _borderColor = value;
                this.Invalidate();
            }
        }

        [Category("QLFUI"), Description("阴影颜色,BorderStyle为FixedSingle有效")]
        public Color ShadowColor
        {
            get { return _shadowColor; }
            set
            {
                _shadowColor = value;
                this.Invalidate();
            }
        }

        [Category("QLFUI"), Description("显示的前端的图片")]
        public Image ForeImage
        {
            get { return pic.Image; }
            set
            {
                _foreImage = value;
                pic.Image = _foreImage;
                Invalidate();
            }
        }

        [Category("QLFUI"), Description("文字")]
        public string Caption
        {
            get
            {
                return txt.Text;
            }
            set
            {
                txt.Text = value;
                Invalidate();
            }
        }

        [Category("行为"), Description("是否多行显示")]
        public bool Multiline
        {
            get { return txt.Multiline; }
            set
            {
                txt.Multiline = value;
            }
        }

        [Category("行为"), Description("是否以密码形式显示字符")]
        public bool UseSystemPasswordChar
        {
            get { return txt.UseSystemPasswordChar; }
            set
            {
                txt.UseSystemPasswordChar = value;
            }
        }

        [Category("QLFUI"), Description("水印文字")]
        public string WaterMark
        {
            get { return _waterMark; }
            set
            {
                _waterMark = value;
                Invalidate();
            }
        }

        [Category("QLFUI"), Description("水印颜色")]
        public Color WaterMarkColor
        {
            get { return _waterMarkColor; }
            set
            {
                _waterMarkColor = value;
                Invalidate();
            }
        }

        #region 需要被隐藏的属性

        [Browsable(false)]
        public new BorderStyle BorderStyle
        {
            get { return BorderStyle.None; }
        }

        [Browsable(false)]
        public new Color BackColor
        {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }

        [Browsable(false)]
        public new Image BackgroundImage
        {
            get { return null; }
        }

        [Browsable(false)]
        public new ImageLayout BackgroundImageLayout
        {
            get { return base.BackgroundImageLayout; }
            set { base.BackgroundImageLayout = value; }
        }

        #endregion

        [Category("QLFUI"), Description("边角弯曲的角度(1-10),数值越大越弯曲")]
        public int Radius
        {
            get { return _radius; }
            set
            {
                if (value > 10)
                {
                    value = 10;
                }
                if (value < 0)
                {
                    value = 0;
                }
                _radius = value;
                this.Invalidate();
            }
        }

        [Browsable(true)]
        [Category("外观"), Description("文本颜色")]
        public new Color ForeColor
        {
            get { return _foreColor; }
            set { _foreColor = value; }
        }

        [Browsable(true)]
        [Category("外观"), Description("鼠标形状")]
        public new Cursor Cursor
        {
            get { return txt.Cursor; }
            set { txt.Cursor = value; }
        }

        [Category("行为"), Description("自动提示方式")]
        public AutoCompleteMode AutoCompleteMode
        {
            get { return txt.AutoCompleteMode; }
            set { txt.AutoCompleteMode = value; }
        }

        [Category("行为"), Description("自动提示类型")]
        public AutoCompleteSource AutoCompleteSource
        {
            get { return txt.AutoCompleteSource; }
            set { txt.AutoCompleteSource = value; }
        }

        #endregion

        #region - 构造函数 -

        public TextBoxEx()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            InitializeComponent();

            pic.BorderStyle = BorderStyle.None;
            BackColor = Color.Transparent;
            pic.SizeMode = PictureBoxSizeMode.StretchImage;


            txt.MouseEnter += new EventHandler(TextBoxEx2_MouseEnter);
            txt.MouseLeave += new EventHandler(TextBoxEx2_MouseLeave);
            pic.MouseEnter += new EventHandler(TextBoxEx2_MouseEnter);
            pic.MouseLeave += new EventHandler(TextBoxEx2_MouseLeave);
            txt.LostFocus += new EventHandler(Txt_LostFocus);
            txt.GotFocus += new EventHandler(Txt_GotFocus);
            pic.BackColor = Color.White;  //不设置成白色则边框会一同加阴影
        }

        #endregion

        #region - 事件 -

        private void TextBoxEx_Load(object sender, EventArgs e)
        {
            SetWaterMark();
        }

        private void TextBoxEx2_MouseEnter(object sender, EventArgs e)
        {
            _isFouse = true;
            this.Invalidate();
        }

        private void TextBoxEx2_MouseLeave(object sender, EventArgs e)
        {
            _isFouse = false;
            this.Invalidate();
        }

        private void Txt_GotFocus(object sender, EventArgs e)
        {
            if (txt.Text == @" " + WaterMark)
            {
                //获得焦点，切换正常文字等待填写
                txt.ForeColor = ForeColor;
                txt.Text = "";
            }
        }

        private void Txt_LostFocus(object sender, EventArgs e)
        {
            SetWaterMark();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;

            CalculateSizeAndPosition();
            Draw(new Rectangle(0, 0, Width, Height), g);

            base.OnPaint(e);
        }

        #endregion

        #region - 帮助方法 -

        private void SetWaterMark()
        {
            if (_waterMark != null && (txt.Text == "" || txt.Text == @" " + WaterMark))   //用户没有输入
            {
                txt.ForeColor = _waterMarkColor;
                txt.Text = @" " + WaterMark;
            }
            else
            {
                txt.ForeColor = ForeColor;
            }
        }

        private void CalculateSizeAndPosition()
        {
            if (ForeImage != null)
            {
                //图片大小固定为16
                pic.Height = pic.Width = 16;
                pic.Top = pic.Left = 3;
                txt.Width = Width - pic.Width - 12;
                txt.Location = new Point(16 + 3 + 3, 6);
            }
            else
            {
                pic.Left = -40;  //隐藏图片
                txt.Width = Width - 9;
                txt.Location = new Point(3, 6);
            }

            //单行
            if (!txt.Multiline)
            {
                Height = txt.Height + 9;
            }
            else
            {
                txt.Height = Height - 9;
            }
        }

        private void Draw(Rectangle rectangle, Graphics g)
        {
            //画白色背景
            using (SolidBrush backgroundBrush = new SolidBrush(Color.White))
            {
                g.FillRectangle(backgroundBrush, 2, 2, this.Width - 5, this.Height - 4);
            }

            //画边框
            using (Pen borderPen = new Pen(_borderColor))
            {
                if (_radius == 0)
                {
                    g.DrawRectangle(borderPen, new Rectangle(rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 3, rectangle.Height - 3));
                }
                else
                {
                    g.DrawPath(borderPen, DrawRoundRect(rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 3, rectangle.Height - 2, _radius));
                }
            }

            //画阴影
            Color drawShadowColor = _shadowColor;
            if (!_isFouse)
            {
                drawShadowColor = Color.Transparent;
            }
            using (Pen shadowPen = new Pen(drawShadowColor))
            {
                if (_radius == 0)
                {
                    g.DrawRectangle(shadowPen, new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1));
                }
                else
                {
                    g.DrawPath(shadowPen, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, _radius));
                }
            }
        }

        #endregion
    }
}
